package cn.cgg.asynchronous;

import cn.cgg.Constants;
import io.netty.util.CharsetUtil;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * 异步消息是发送者发送消息，无需等待发送结果就可以再发送第二条消息，它是通过回调的方式来获取到消息的发送结果，消息可靠性高
 * */
public class Producer {
    public static void main(String[] args) throws Exception {
        //创建生产者数组
        DefaultMQProducer producer = new DefaultMQProducer("hello-world");
        //设置nameserver地址
        producer.setNamesrvAddr(Constants.NAME_SERVER_URL);
        //启动product实例
        producer.start();

        //发送消息
        producer.send(
                //创建消息对象
                new Message("log-topic", "info_tag", "谌媾觏脑壳真大".getBytes(CharsetUtil.UTF_8)),
                //添加发送回调
                new SendCallback() {

                    //发送成功结果处理
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        System.out.println(sendResult);
                    }
                    //发送异常结果处理
                    @Override
                    public void onException(Throwable throwable) {
                        System.out.println("发送异常："+throwable.getMessage());
                    }
                }
        );
        //关闭product
        System.out.println("发送完毕,结果:");

    }
}
